package sudoku.client.presen.componentes;

import java.awt.Color;
import java.rmi.RemoteException;

import javax.swing.JPanel;
import javax.swing.JTextField;

import sudoku.client.presen.IVentanaJugador;
/**
 * @author Administrador
 */
public class JPCuadricula extends JPanel {

	private JTextField jt11 = null;
	private JTextField jt12 = null;
	private JTextField jt13 = null;
	private JTextField jt21 = null;
	private JTextField jt22 = null;
	private JTextField jt23 = null;
	private JTextField jt31 = null;
	private JTextField jt32 = null;
	private JTextField jt33 = null;
	private byte idCuadricula;
	/**
	 * This is the default constructor
	 */
	public JPCuadricula() {
		super();
		initialize();
	}
	/**
	 * @param idCuadricula The idCuadricula to set.
	 */
	public void setIdCuadricula(byte idCuadricula) {
		this.idCuadricula = idCuadricula;
	}
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private  void initialize() {
		this.setLayout(null);
		this.setSize(97, 87);
		this.add(getJt11(), null);
		this.add(getJt12(), null);
		this.add(getJt13(), null);
		this.add(getJt21(), null);
		this.add(getJt22(), null);
		this.add(getJt23(), null);
		this.add(getJt31(), null);
		this.add(getJt32(), null);
		this.add(getJt33(), null);
	}
	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */    
	private JTextField getJt11() {
		if (jt11 == null) {
			jt11 = new JTextField();
			jt11.setBounds(6, 5, 28, 25);
			jt11.setName("");
			jt11.addKeyListener(new java.awt.event.KeyAdapter() {   
				public void keyReleased(java.awt.event.KeyEvent e) {      
					jugadorColoca(jt11, (byte) 0, (byte) 0);
				}
			});
		}
		return jt11;
	}
	/**
	 * 
	 */
	protected void jugadorColoca(JTextField jtf, byte fila, byte columna) {
		String sNumero=jtf.getText();
		sNumero=sNumero.trim();
		if (sNumero.length()>0) {
			try {
				byte numero=Byte.parseByte(sNumero);
				this.ventanaJugador.jugadorColoca(this.idCuadricula, numero, fila, columna);
			}
			catch (Exception ex) {
			}
		} else
			try {
				this.ventanaJugador.jugadorQuita(this.idCuadricula, fila, columna);
			} catch (RemoteException e) {
			}
	}
	/**
	 * This method initializes jTextField1	
	 * 	
	 * @return javax.swing.JTextField	
	 */    
	private JTextField getJt12() {
		if (jt12 == null) {
			jt12 = new JTextField();
			jt12.setBounds(34, 5, 28, 25);
			jt12.addKeyListener(new java.awt.event.KeyAdapter() { 
				public void keyReleased(java.awt.event.KeyEvent e) {    
					jugadorColoca(jt12, (byte) 0, (byte) 1);
				}
			});
		}
		return jt12;
	}
	/**
	 * This method initializes jTextField2	
	 * 	
	 * @return javax.swing.JTextField	
	 */    
	private JTextField getJt13() {
		if (jt13 == null) {
			jt13 = new JTextField();
			jt13.setBounds(62, 5, 28, 25);
			jt13.addKeyListener(new java.awt.event.KeyAdapter() { 
				public void keyReleased(java.awt.event.KeyEvent e) {
					jugadorColoca(jt13, (byte) 0, (byte) 2);
				}
			});
		}
		return jt13;
	}
	/**
	 * This method initializes jTextField3	
	 * 	
	 * @return javax.swing.JTextField	
	 */    
	private JTextField getJt21() {
		if (jt21 == null) {
			jt21 = new JTextField();
			jt21.setBounds(6, 30, 28, 25);
			jt21.addKeyListener(new java.awt.event.KeyAdapter() { 
				public void keyReleased(java.awt.event.KeyEvent e) {
					jugadorColoca(jt21, (byte) 1, (byte) 0);
				}
			});
		}
		return jt21;
	}
	/**
	 * This method initializes jTextField4	
	 * 	
	 * @return javax.swing.JTextField	
	 */    
	private JTextField getJt22() {
		if (jt22 == null) {
			jt22 = new JTextField();
			jt22.setBounds(34, 30, 28, 25);
			jt22.addKeyListener(new java.awt.event.KeyAdapter() { 
				public void keyReleased(java.awt.event.KeyEvent e) {   
					jugadorColoca(jt22, (byte) 1, (byte) 1);
				}
			});
		}
		return jt22;
	}
	/**
	 * This method initializes jTextField5	
	 * 	
	 * @return javax.swing.JTextField	
	 */    
	private JTextField getJt23() {
		if (jt23 == null) {
			jt23 = new JTextField();
			jt23.setBounds(62, 30, 28, 25);
			jt23.addKeyListener(new java.awt.event.KeyAdapter() { 
				public void keyReleased(java.awt.event.KeyEvent e) {    
					jugadorColoca(jt23, (byte) 1, (byte) 2);
				}
			});
		}
		return jt23;
	}
	/**
	 * This method initializes jTextField6	
	 * 	
	 * @return javax.swing.JTextField	
	 */    
	private JTextField getJt31() {
		if (jt31 == null) {
			jt31 = new JTextField();
			jt31.setBounds(6, 55, 28, 25);
			jt31.addKeyListener(new java.awt.event.KeyAdapter() { 
				public void keyReleased(java.awt.event.KeyEvent e) {    
					jugadorColoca(jt31, (byte) 2, (byte) 0);
				}
			});
		}
		return jt31;
	}
	/**
	 * This method initializes jTextField7	
	 * 	
	 * @return javax.swing.JTextField	
	 */    
	private JTextField getJt32() {
		if (jt32 == null) {
			jt32 = new JTextField();
			jt32.setBounds(34, 55, 28, 25);
			jt32.addKeyListener(new java.awt.event.KeyAdapter() { 
				public void keyReleased(java.awt.event.KeyEvent e) {    
					jugadorColoca(jt32, (byte) 2, (byte) 1);
				}
			});
		}
		return jt32;
	}
	/**
	 * This method initializes jTextField8	
	 * 	
	 * @return javax.swing.JTextField	
	 */    
	private JTextField getJt33() {
		if (jt33 == null) {
			jt33 = new JTextField();
			jt33.setBounds(62, 55, 28, 25);
			jt33.addKeyListener(new java.awt.event.KeyAdapter() { 
				public void keyReleased(java.awt.event.KeyEvent e) {    
					jugadorColoca(jt33, (byte) 2, (byte) 2);
				}
			});
		}
		return jt33;
	}
	/**
	 * @param c
	 * @param filaCasilla
	 * @param columnaCasilla
	 */
	public void putValue(char numero, int filaCasilla, int columnaCasilla) {
		JTextField j=null; 
		if (filaCasilla==0) {
			if (columnaCasilla==0)
				j=this.jt11;
			else if (columnaCasilla==1)
				j=this.jt12;
			else if (columnaCasilla==2)
				j=this.jt13;
		} else if (filaCasilla==1) {
			if (columnaCasilla==0)
				j=this.jt21;
			else if (columnaCasilla==1)
				j=this.jt22;
			else if (columnaCasilla==2)
				j=this.jt23;
		} else if (filaCasilla==2) {
			if (columnaCasilla==0)
				j=this.jt31;
			else if (columnaCasilla==1)
				j=this.jt32;
			else if (columnaCasilla==2)
				j=this.jt33;
		} 
		String sNumero="" + numero;
		if (sNumero.equals("0"))
			sNumero="";
		j.setText(sNumero);
	}

	/**
	 *  
	 * @uml.property name="ventanaJugador"
	 * 
	 */
	public void setVentanaJugador(IVentanaJugador ventanaJugador) {
		this.ventanaJugador = ventanaJugador;
	}
/**
	 * @return
	 */
	public String getNumeros() {
		String result="";
		result+=(this.jt11.getText().trim().length()==0 ? "0" : this.jt11.getText().trim()) +
				(this.jt12.getText().trim().length()==0 ? "0" : this.jt12.getText().trim()) +
				(this.jt13.getText().trim().length()==0 ? "0" : this.jt13.getText().trim()) +
				(this.jt21.getText().trim().length()==0 ? "0" : this.jt21.getText().trim()) +
				(this.jt22.getText().trim().length()==0 ? "0" : this.jt22.getText().trim()) +
				(this.jt23.getText().trim().length()==0 ? "0" : this.jt23.getText().trim()) +
				(this.jt31.getText().trim().length()==0 ? "0" : this.jt31.getText().trim()) +
				(this.jt32.getText().trim().length()==0 ? "0" : this.jt32.getText().trim()) +
				(this.jt33.getText().trim().length()==0 ? "0" : this.jt33.getText().trim());
		return result;
	}

	/**
	 *  
	 * @uml.property name="ventanaJugador"
	 * @uml.associationEnd inverse="jPCuadricula:sudoku.client.presen.IVentanaJugador" multiplicity="(0 1)"
	 * 
	 */
	private IVentanaJugador ventanaJugador;

	/**
	 *  
	 * @uml.property name="ventanaJugador"
	 * 
	 */
	public IVentanaJugador getVentanaJugador() {
		return ventanaJugador;
	}
	public void pinta(int numeroArea, int filaCasilla, int columnaCasilla) {
		Color [] paleta = new Color [10]; 
		JTextField j=null; 
		
		paleta [0] = new Color (200,20,40);
		paleta [1] = new Color (20,20,200);
		paleta [2] = new Color (100,100,100);
		paleta [3] = new Color (150,40,30);
		paleta [4] = new Color (40,150,150);
		paleta [5] = new Color (100,200,255);
		paleta [6] = new Color (100,200,240);
		paleta [7] = new Color (200,200,200);
		paleta [8] = new Color (255,255,100);
		paleta [9] = new Color (120,200,255);
		
		if (filaCasilla==0) {
			if (columnaCasilla==0)
				j=this.jt11;
			else if (columnaCasilla==1)
				j=this.jt12;
			else if (columnaCasilla==2)
				j=this.jt13;
		} else if (filaCasilla==1) {
			if (columnaCasilla==0)
				j=this.jt21;
			else if (columnaCasilla==1)
				j=this.jt22;
			else if (columnaCasilla==2)
				j=this.jt23;
		} else if (filaCasilla==2) {
			if (columnaCasilla==0)
				j=this.jt31;
			else if (columnaCasilla==1)
				j=this.jt32;
			else if (columnaCasilla==2)
				j=this.jt33;
		}
		
		j.setBackground(paleta[numeroArea%10]);

		
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
